package com.jwds.digital.bios.business.api.controller;


import com.jwds.digital.bios.business.service.service.belt.BeltCoreHandleLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

/**
 * @Description controller
 * @Author xiao  xun
 * @Date 2024-12-25
 */
@RestController
@RequestMapping("/api/v1/controller")
public class controller {
    @Autowired
    private BeltCoreHandleLineService beltCoreHandleLineService;
    /**
     * 小时级别表参数收集 每一小时一次
     */

    @PostMapping("/calculate-one-hour-belt-performance")
    public void calculateOneHourBeltPerformance() {
        // 计算当前时间前一小时的时间范围
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startTime = now.minusHours(1).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime endTime = now.withMinute(0).withSecond(0).withNano(0);

        // 调用 doOneHourCalculateByOneMinData 方法
        beltCoreHandleLineService.doOneHourCalculateByOneMinData(
                startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")),
                endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
        );
    }

    /**
     * 天级别表参数收集 每一天一次
     */

    @PostMapping("/doOneHourLineCalculateByOneDayDataJob")
    public void doOneHourLineCalculateByOneDayDataJob() {
        // 获取前一天的日期
        LocalDate yesterday = LocalDate.now().minusDays(1);


        // 设置开始时间和结束时间（前一天的 00:00:00 到 23:59:59）
        LocalDateTime startTime = LocalDateTime.of(yesterday, LocalTime.of(0, 0, 0)); // 明确指定秒为 0
        LocalDateTime endTime = LocalDateTime.of(yesterday, LocalTime.of(23, 59, 59)); // 明确指定秒为 59

        // 格式化为字符串
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String startTimeStr = startTime.format(formatter);
        String endTimeStr = endTime.format(formatter);

        // 调用业务方法
        beltCoreHandleLineService.doOneHourLineCalculateByOneDayData(startTimeStr, endTimeStr);
    }
    /**
     * 周级别表参数收集 每一周一次
     */

    @PostMapping("/calculateOneWeekBeltPerformance")
    public void calculateOneWeekBeltPerformance() {
        // 计算上周的时间范围
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime lastSunday = now.with(TemporalAdjusters.previous(DayOfWeek.SUNDAY)); // 获取上周日
        LocalDateTime lastMonday = lastSunday.minusDays(6); // 获取上周一
        LocalDateTime startTime = lastMonday.withHour(0).withMinute(0).withSecond(0);
        LocalDateTime endTime = lastSunday.withHour(23).withMinute(59).withSecond(59);

        // 调用 doOneWeekLineCalculateByOneDayData 方法
        beltCoreHandleLineService.doOneWeekLineCalculateByOneDayData(
                startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")),
                endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
        );
    }

    /**
     * 每月一号凌晨0点执行，计算上月皮带效能数据
     */

    @PostMapping("/calculateOneMonthBeltPerformance")
    public void calculateOneMonthBeltPerformance() {
        // 计算上月的时间范围
        LocalDate now = LocalDate.now();
        LocalDate firstDayOfLastMonth = now.minusMonths(1).with(TemporalAdjusters.firstDayOfMonth());
        LocalDate lastDayOfLastMonth = now.minusMonths(1).with(TemporalAdjusters.lastDayOfMonth());

        LocalDateTime startTime = firstDayOfLastMonth.atStartOfDay();
        LocalDateTime endTime = lastDayOfLastMonth.atTime(23, 59, 59);

        // 调用 doOneMonthLineCalculateByOneWeekData 方法
        beltCoreHandleLineService.doOneMonthLineCalculateByOneWeekData(
                startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")),
                endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
        );
    }

}
